<template>
  <div class="container">
    <!-- 查询功能 -->
    <div class="header">
      <el-form ref="form" :model="form" label-width="80px" inline>
        <el-form-item>
          <el-select
            v-model="form.region"
            placeholder="请选择查询条件"
            class="query"
            style="width: 160px; margin-right: 20px"
          >
            <el-option label="店铺名称" value="name"></el-option>
            <el-option label="店铺状态" value="isAvail"></el-option>
          </el-select>
          <el-form-item>
            <el-input v-model="form.name"></el-input>
          </el-form-item>
          <el-button type="primary" class="referBtn" @click="searchHandle"
            >查询</el-button
          >
          <el-button type="primary" class="referBtn" @click="resetHandle"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <!-- 表格数据 -->
    <el-table
      :data="tableData"
      border
      style="width: 100%"
      :header-cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column type="index" label="序号" align="center" width="60">
      </el-table-column>
      <el-table-column prop="name" label="店铺名称" align="center" width="140">
      </el-table-column>
      <el-table-column prop="address" label="店铺地址" align="center">
      </el-table-column>
      <el-table-column prop="telephone" label="联系电话" align="center">
      </el-table-column>
      <el-table-column prop="desc" label="店铺描述" width="200" align="center">
      </el-table-column>
      <el-table-column
        prop="isAvail"
        label="店铺状态"
        width="100"
        align="center"
      >
        <template slot-scope="scope">
          <el-tag
            :type="
              scope.row.isAvail === '审核中'
                ? 'primary'
                : 'success' && scope.row.isAvail === '未通过'
                ? 'danger'
                : 'success' && scope.row.isAvail === '已通过'
                ? 'success'
                : 'info'
            "
            disable-transitions
            >{{ scope.row.isAvail }}</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button @click="handleEdit(scope.row)" type="primary" size="small"
            >更改状态</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 更改状态 -->
    <el-dialog
      title="状态更改"
      :visible.sync="dialogVisible"
      width="20%"
      :before-close="handleClose"
    >
      <el-select v-model="boxValue" placeholder="请选择" class="box">
        <el-option
          v-for="(item, index) in options"
          :key="index"
          :label="item.text"
          :value="item.value"
        >
        </el-option>
      </el-select>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmHandle()">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 分页 -->
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="5"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import axios from "axios";
import { putShop, getAdminList } from "@/api/admin";
import request from "@/utils/request";
export default {
  data() {
    const generateData = _ => {
      const data = [];
      for (let i = 1; i <= 15; i++) {
        data.push({
          key: i,
          label: `${i}`
        });
      }
      return data;
    };
    return {
      disabled: true,
      data: generateData(),
      beforeValue: [],
      afterValue: [],
      renderFunc(h, option) {
        return <span>{option.label}</span>;
      },
      dialogVisible: false, // 弹出框状态
      options: [
        { text: "已通过", value: "已通过" },
        { text: "未通过", value: "未通过" },
        { text: "已注销", value: "已注销" }
      ],
      boxValue: "", //弹出框选择
      redactInfo: "", //编辑信息

      cancellation: true,
      againApply: true,
      visible: false,
      tableData: [],
      dialogUpdateVisible: false,
      dialogVisibleGoods: false,
      newTableData: [],
      form: {
        region: "", // 查询选择项
        name: "" // 查询内容
      },
      currentPage: 0, // 当前显示页
      total: 0, // 总条数
      size: 0, // 每页显示条数
      totalPage: 0, // 总页数
      serial: null, // 序列号
      name: "", // 家居名称
      address: "", // 家居地址
      telephone: "", // 联系电话
      desc: "", // 家居描述
      isAvail: "", // 家居状态
      goods: "", // 商品栏
      goodsArr: [],
      servicesId: [],
      serviceArr: [],
      value: ""
    };
  },
  mounted() {
    this.search();
  },
  methods: {
    // 搜索方法
    search(obj) {
      request.get("shops/all", { params: obj }).then(res => {
        this.tableData = res.rows;
        this.currentPage = parseInt(res.current);
        this.total = parseInt(res.total);
        this.size = parseInt(res.size);
        this.totalPage = parseInt(res.totalPage);
      });
    },
    // 查询选项
    filterTag(value, row) {
      return row.state === value;
    },
    // 搜索内容
    searchHandle() {
      let obj = {
        type: this.form.region,
        value: this.form.name
      };
      this.search(obj);
      this.form.region = this.form.name = "";
    },
    // 重置按钮
    resetHandle() {
      this.search();
      this.form.region = this.form.name = "";
    },
    // 显示条数更换
    handleSizeChange(val) {
      let obj = { size: val };
      this.search(obj);
    },
    // 每页点击更换
    handleCurrentChange(val) {
      let obj = { current: val };
      this.search(obj);
    },
    // 弹出框右上角关闭
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then(_ => {
          done();
        })
        .catch(_ => {});
    },
    // 弹出框确认按钮
    confirmHandle() {
      if (this.boxValue) {
        this.redactInfo.status = this.boxValue;
      }
      let arr = [
        { id: this.redactInfo._id },
        { status: this.redactInfo.status }
      ];
      putShop(arr);
      this.search();
      this.dialogVisible = false;
    },
    // 更改状态
    handleEdit(row) {
      this.redactInfo = row; // 当前行信息
      this.boxValue = "";
      this.dialogVisible = true;
    }
  }
};
</script>

<style>
.container {
  padding: 50px;
}
.header {
  display: flex;
  justify-content: space-between;
}
.right {
  text-align: center;
}
.block {
  margin-top: 20px;
}
.checkbox {
  display: none;
}
</style>
